﻿using Microsoft.EntityFrameworkCore;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using tju.cme.BookStore2024.webapi.Models.EF;

namespace tju.cme.BookStore2024.webapi.Models.Tools
{
    public class Util
    {     /// <summary>
          /// Copies the data of one object to another. The target object 'pulls' properties of the first.
          /// This any matching properties are written to the target.
          ///
          /// The object copy is a shallow copy only. Any nested types will be copied as
          /// whole values rather than individual property assignments (ie. via assignment)
          /// </summary>
          /// <param name="source">The source object to copy from</param>
          /// <param name="target">The object to copy to</param>
          /// <param name="excludedProperties">A comma delimited list of properties that should not be copied</param>
        public static void CopyObjectData(object source, object target, string excludedProperties = "")
        {
            //string[] excluded = null;
            var excluded = new List<string>();
            if (!string.IsNullOrEmpty(excludedProperties))
                excluded = excludedProperties.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            var miT = target.GetType().GetMembers();
            foreach (var field in miT.Where(m => m.MemberType == MemberTypes.Property || m.MemberType == MemberTypes.Field))
            {
                var name = field.Name;
                // Skip over any property exceptions
                if (!string.IsNullOrEmpty(excludedProperties) && excluded.Contains(name))
                    continue;
                if (field.MemberType == MemberTypes.Field)
                {
                    var sourceField = source.GetType().GetField(name);
                    if (sourceField == null)
                        continue;
                    var sourceValue = sourceField.GetValue(source);
                    ((FieldInfo)field).SetValue(target, sourceValue);
                }
                else if (field.MemberType == MemberTypes.Property)
                {
                    var piTarget = field as PropertyInfo;
                    var sourceField = source.GetType().GetProperty(name);
                    if (sourceField == null)
                        continue;
                    if (piTarget != null && piTarget.CanWrite && sourceField.CanRead)
                    {
                        var sourceValue = sourceField.GetValue(source, null);
                        piTarget.SetValue(target, sourceValue, null);
                    }
                }
            }
        }

        /// <summary>
        /// MD5加密字符串（32位大写）
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5(string? source)
        {
            using MD5CryptoServiceProvider md5 = new();
            byte[] bytes = Encoding.UTF8.GetBytes(source ?? "");
            string result = BitConverter.ToString(md5.ComputeHash(bytes));
            return result.Replace("-", "");
        }

        /// <summary>
        /// 获得字节数组对应的Md5值
        /// </summary>
        /// <param name="bytedata"></param>
        /// <returns></returns>
        public static string GetMD5Hash(byte[] bytedata)
        {
            try
            {
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(bytedata);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("Md5错误:" + ex.Message);
            }
        }



        
    }
}
